/*
 * @lc app=leetcode.cn id=14 lang=golang
 * @lcpr version=30118
 *
 * [14] 最长公共前缀
 */

// @lcpr-template-start

// @lcpr-template-end
// @lc code=start
func longestCommonPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}
	if len(strs) == 1 {
		return strs[0]
	}

	prefix := strs[0]
	for i := 1; i < len(strs) && len(prefix) > 0; i++ {
		prefix = commonPrefix(prefix, strs[i])
	}
	return prefix
}

func commonPrefix(s1, s2 string) string {
	n := len(s1)
	if n > len(s2) {
		n = len(s2)
	}
	for i := 0; i < n; i++ {
		if s1[i] != s2[i] {
			return s1[:i]
		}
	}
	return s1[:n]
}

// @lc code=end

/*
// @lcpr case=start
// ["flower","flow","flight"]\n
// @lcpr case=end

// @lcpr case=start
// ["dog","racecar","car"]\n
// @lcpr case=end

*/

